#include "AnalyseArticlelist.h"
#include <utils/XString.h>
#include <exception/NpException.h>

using namespace std;
using namespace npexception;
using namespace ruleconfiguration;
string& AnalyseArticleList::operator [](int index)
{
  throwExIf(index < 0 || index >= m_listLength, INVALID_LIST_INDEX);
  return m_articleList.at(index);
}

std::string& AnalyseArticleList::getNovelListUrl()
{
  RegexInfo* reg =  m_rule.getRegex( "NovelListUrl");
  return reg->Pattern;
}

void AnalyseArticleList::analyseUrlList()
{
  reset();
  static string url;
  RegexInfo* regUrlList = m_rule.getRegex("NovelList_GetNovelKey");
  //正则匹配出小说链接集合
  std::string::const_iterator itr_begin = m_content.begin();
  std::string::const_iterator itr_end = m_content.end();
  boost::smatch matchs;
  boost::regex regexExpress(regUrlList->Pattern);
  //遍历匹配出来的小说连接，替换NovelUrl
  RegexInfo* regNovel = m_rule.getRegex("NovelUrl");
  m_listLength = 0;
  while(boost::regex_search(itr_begin, itr_end, matchs, regexExpress, regNovel->Options))
  {
      url.assign(regNovel->Pattern);
      XString::replaceAll(url, "{NovelKey}", matchs[1]);
      m_articleList.push_back(url);
      itr_begin = matchs[0].second;
      m_listLength++;
  }
}
